package com.ruoyi.blindBoxManage.service.impl;

import com.ruoyi.blindBoxManage.domain.BlindBoxProduct;
import com.ruoyi.blindBoxManage.domain.BlindProduct;
import com.ruoyi.blindBoxManage.domain.Box;
import com.ruoyi.blindBoxManage.mapper.BlindBoxProductMapper;
import com.ruoyi.blindBoxManage.mapper.BlindBoxPublicProductMapper;
import com.ruoyi.blindBoxManage.mapper.BlindProductMapper;
import com.ruoyi.blindBoxManage.mapper.BoxMapper;
import com.ruoyi.blindBoxManage.service.IBoxService;
import com.ruoyi.common.redis.RedisUtil;
import com.ruoyi.common.utils.DateUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 盲盒箱子配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Slf4j
@Service
public class BoxServiceImpl implements IBoxService {
    @Autowired
    private BoxMapper boxMapper;
    @Resource
    private BlindBoxProductMapper blindBoxProductMapper;
    @Resource
    private BlindBoxPublicProductMapper blindBoxPublicProductMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private BlindProductMapper blindProductMapper;

    /**
     * 查询盲盒箱子配置
     *
     * @param id 盲盒箱子配置主键
     * @return 盲盒箱子配置
     */
    @Override
    public Box selectBoxById(Long id) {
        return boxMapper.selectBoxById(id);
    }

    /**
     * 查询盲盒箱子配置列表
     *
     * @param box 盲盒箱子配置
     * @return 盲盒箱子配置
     */
    @Override
    public List<Box> selectBoxList(Box box) {
        return boxMapper.selectBoxList(box);
    }

    /**
     * 新增盲盒箱子配置
     *
     * @param box 盲盒箱子配置
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int insertBox(Box box) {
        //查询是否已经有箱子
        Box lastBox = boxMapper.selectLastBoxByBlindBoxId(box.getBlindBoxId());
        int lastBoxCount = 1;
        if (lastBox != null) {
            lastBoxCount = Integer.parseInt(lastBox.getName()) + 1;
        }
        List<BlindProduct> blindProducts = blindProductMapper.selectProductList(box.getBlindBoxId());

        for (int i = 0; i < box.getBoxCount(); i++) {
            Box insertBox = new Box();
            insertBox.setBlindBoxId(box.getBlindBoxId());
            insertBox.setName(String.valueOf(lastBoxCount + i));
            insertBox.setPicture(box.getPicture());
            insertBox.setMinCount(box.getMinCount());
            insertBox.setWaitTime(box.getWaitTime());
            insertBox.setPartitionRank(box.getPartitionRank());
            insertBox.setPartitionPoints(box.getPartitionPoints());
            //if (lastBoxCount+i == 1) {
            insertBox.setIsUnlock(box.getIsUnlock());
            //} else {
            //    insertBox.setIsUnlock(0);
            //}
            boxMapper.insertBox(insertBox);

            if (!CollectionUtils.isEmpty(blindProducts)) {
                for (BlindProduct blindProduct : blindProducts) {
                    BlindBoxProduct insertBlindBoxProduct = new BlindBoxProduct();
                    BeanUtils.copyProperties(blindProduct, insertBlindBoxProduct);
                    insertBlindBoxProduct.setBoxId(insertBox.getId());
                    blindBoxProductMapper.insertBlindBoxProduct(insertBlindBoxProduct);
                }
            }
        }
        return 1;
    }

    /**
     * 修改盲盒箱子配置
     *
     * @param box 盲盒箱子配置
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int updateBox(Box box) {
        Integer count = boxMapper.selectCountByBlindBoxIdAndBoxNameAndId(box.getBlindBoxId(), box.getName(), box.getId());
        if (count > 0) {
            throw new RuntimeException("该箱子已存在");
        }
        box.setUpdateTime(DateUtils.getNowDate());
        return boxMapper.updateBox(box);
    }

    /**
     * 批量删除盲盒箱子配置
     *
     * @param ids 需要删除的盲盒箱子配置主键
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int deleteBoxByIds(Long[] ids) {
        for (Long id : ids) {
            //删除盲盒箱子绑定的商品
            blindBoxProductMapper.deleteByBoxId(id);
            String drawCount = "drawCount_%s_%s";
            Box box = boxMapper.selectBoxById(id);
            String redisKey = String.format(drawCount, box.getBlindBoxId(), id);
            redisUtil.del(redisKey);
        }
        return boxMapper.deleteBoxByIds(ids);
    }

    /**
     * 删除盲盒箱子配置信息
     *
     * @param id 盲盒箱子配置主键
     * @return 结果
     */
    @Override
    public int deleteBoxById(Long id) {
        return boxMapper.deleteBoxById(id);
    }
}
